package com.woniuxy.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.crm.entity.Order;
import com.woniuxy.crm.entity.OrderItem;
import com.woniuxy.crm.result.Result;
import com.woniuxy.crm.service.IOrderItemService;
import com.woniuxy.crm.service.IOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单表 前端控制器
 *
 * @author 郑鹏
 * @since 2024-08-12
 */
@RestController
@RequestMapping("/admin/order")
@Tag(name = "订单表接口")
public class OrderController {

    @Autowired
    private IOrderService orderService;
    @Autowired
  private IOrderItemService orderItemService;

    // 新增或者更新
    @PostMapping("/saveOrUpdate")
    @Operation(summary = "新增或者修改")
    public Result save(@RequestBody Order order) {
        orderService.saveOrUpdate(order);
        return Result.ok();
    }

    @DeleteMapping("/deleteById")
    @Operation(summary = "根据id删除")
    public Result delete(@RequestParam Long id) {
        orderService.removeById(id);
        LambdaQueryWrapper<OrderItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderItem::getOrderId, id);
        orderItemService.remove(lambdaQueryWrapper);

        return Result.ok();
    }

//    @PostMapping("/del/batch")
//    @Operation(summary = "批量删除")
//    public boolean deleteBatch(@RequestBody List<Integer> ids) {
//        return orderService.removeByIds(ids);
//    }

    @GetMapping("list")
    @Operation(summary = "查询全部")
    public Result<List<Order>> findAll() {
        List<Order> list = orderService.list();
        return Result.ok(list);
    }

    @GetMapping("/getById")
    @Operation(summary = "根据id查询")
    public Result<Order> findOne(@RequestParam Long id) {
        Order order = orderService.getById(id);
        return Result.ok(order);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Result<Page<Order>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Order::getId);
        Page<Order> page = orderService.page(new Page<>(pageNum, pageSize), queryWrapper);


        return Result.ok(page);

    }

}

